package com.share.device.controller;

import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.share.common.core.exception.ServiceException;
import com.share.common.core.web.controller.BaseController;
import com.share.common.core.web.domain.AjaxResult;
import com.share.common.core.web.page.TableDataInfo;
import com.share.common.log.annotation.Log;
import com.share.common.log.enums.BusinessType;
import com.share.common.security.annotation.RequiresPermissions;
import com.share.device.domain.Station;
import com.share.device.service.IStationService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

/**
 * @author ZhouShuang
 * @version 17
 * @Title
 * @date 2025/10/11 下午5:05
 */
@Tag(name = "站点接口管理")
@RestController
@RequestMapping("/station")
@RequiredArgsConstructor
public class StationController extends BaseController {

    private final IStationService stationService;

    /**
     * 查询站点列表
     *
     * @param station
     * @return
     */
    @Operation(summary = "查询站点列表")
    @RequiresPermissions("device:station:list")
    @Log(title = "站点")
    @GetMapping("/list")
    public TableDataInfo list(Station station) {
        startPage();
        return getDataTable(stationService.selectStationList(station));
    }

    /**
     * 添加站点
     *
     * @param station
     * @return
     */
    @Operation(summary = "添加站点")
    @RequiresPermissions("device:station:add")
    @Log(title = "站点", businessType = BusinessType.INSERT)
    @PostMapping()
    public AjaxResult add(@RequestBody Station station) {
        return toAjax(stationService.saveStation(station));
    }

    /**
     * 修改站点信息
     *
     * @param station
     * @return
     */
    @Operation(summary = "修改站点信息")
    @RequiresPermissions("device:station:edit")
    @Log(title = "站点", businessType = BusinessType.UPDATE)
    @PutMapping()
    public AjaxResult update(@RequestBody Station station) {
        return toAjax(stationService.updateStation(station));
    }

    /**
     * 获取站点详细信息
     *
     * @param id
     * @return
     */
    @Operation(summary = "获取站点详细信息")
    @RequiresPermissions("device:station:query")
    @Log(title = "站点")
    @GetMapping("/{id}")
    public AjaxResult getInfo(@PathVariable Long id) {
        return success(stationService.getById(id));
    }

    /**
     * 删除站点信息
     *
     * @param ids
     * @return
     */
    @Operation(summary = "删除站点信息")
    @RequiresPermissions("device:station:remove")
    @Log(title = "站点", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids) {
        return toAjax(stationService.removeStationBatchByIds(ids));
    }

    /**
     * 站点设置关联数据
     *
     * @param station
     * @return
     */
    @Operation(summary = "站点设置关联数据")
    @Log(title = "站点")
    @PostMapping("/setData")
    public AjaxResult setData(@RequestBody Station station) {
        return toAjax(stationService.setData(station));
    }

    /**
     * 更新数据到mongodb
     *
     * @return
     */
    @Operation(summary = "更新数据到mongodb")
    @Log(title = "站点")
    @GetMapping("/updateData")
    public AjaxResult updateData() {
        stationService.updateData();
        return success();
    }

    @Operation(summary = "修改站点状态")
    @PutMapping("/updateStatus")
    public AjaxResult updateStatus(@RequestParam("id") Long id, @RequestParam("status") String status) {
        return success(stationService.updateStatus(id, status));
    }

    @Operation(summary = "生成对应柜机的二维码")
    @GetMapping("/generate/{cabinetNo}")
    public void generate(@PathVariable String cabinetNo, HttpServletResponse response) {
        //宽高
        int width = 300;
        int height = 300;
        // 设置响应头
        response.setContentType("image/png");
        response.setHeader("Content-Disposition", "attachment; filename=" + cabinetNo + ".png");
        // 生成二维码
        BufferedImage generate = stationService.generate(cabinetNo, width, height);
        // 输出到响应流
        // 3. 输出图像到响应流
        try (OutputStream out = response.getOutputStream()) {
            ImageIO.write(generate, "png", out);
            out.flush();
        } catch (IOException e) {
            throw new ServiceException("二维码输出失败: " + e.getMessage());
        }
    }
}
